<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Goodsreport extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('Function_m','fun');
		$this->load->model('Goodsreport_m','m');
		$this->load->model('Datareport_m','d');
		$this->pageSize =10;
		$this->size = array('25','30','35','40','45','50','55','60');
		$this->sizest = array('25'=>'XXS','30'=>'XS','35'=>'S','40'=>'M','45'=>'L','50'=>'XL','55'=>'XXL','60'=>'XXXL');
	 }
	/*********
		工厂销售数据统计
	********/
	function showreport()
	{
		$this->pageSize = 50;
		$nowpage = $_REQUEST["pageid"];
	    if ($nowpage == "")
            $nowpage = 1;
		
		
		$where="";
		
		$other = $this->input->get('other');
		if($other)
		{
			$where.=" AND (goods_name like '%".$other."%' or goos_sku like '%".$other."%')";
			$this->tpl->assign("other",$other);
		}
		$d = $this->input->get('d');
		$sales = $this->input->get('sales');
		$sales = intval($sales)?intval($sales):0;
		if($d)
		{
			
			$where.=" AND (num{$d} >= $sales )";
			$this->tpl->assign("d",$d);
			$this->tpl->assign("sales",$sales);
		}
		$model = trim($this->input->get("model"));
		if($model){
			$count = trim($this->config->item("salesgoodscount"));
			$where.= " AND model = '{$model}' AND sku_exists = 0 AND num2 >= {$count} ";
			$this->tpl->assign("model",$model);
			$this->tpl->assign("d",2);
			$this->tpl->assign("sales",$count);
		}

		if($_COOKIE["classid"] ==12)	//工厂1人员
		{
			$where .= " and  locate(11,goos_sku)=1  ";
		}
		
		if($_COOKIE["classid"] ==13)	//工厂2人员
		{
			$where .= " and  locate(12,goos_sku)=1  ";
		}

		$orderyts =  $this->input->get('ordery');
		$by =  $this->input->get('by')?$this->input->get('by'):'num1';
		if($orderyts)
		{
			$orderby =" order by {$by} {$orderyts}";
			if($orderyts=='ASC')
			{
				$this->tpl->assign("orderyts",'DESC');
			}else{
				$this->tpl->assign("orderyts",'ASC');
			}

		}else{
			$this->tpl->assign("orderyts",'ASC');
		}


		
		if(!$orderbysku && !$orderyts)
		{
			$orderby =" order by model ASC , gender asc";
		}
		$list = $this->fun->getalllist('db_factory_salereport',$where,$nowpage,$if_csv,$this->pageSize,$orderby);
		foreach($list["list"] as $key=>$rs)
		{
		}

		$saleskumonth  = trim($this->config->item('saleskumonth'));
		if($saleskumonth)
		{
			$days = explode("|",$saleskumonth);
			$days1 =$days[0];
			$days2 =$days[1];
			$days3 =$days[2];
			
		}else{
			$days1 =30;
			$days2 =60;
			$days3 =90;
		}
		$this->tpl->assign("days1", $days1);
		$this->tpl->assign("days2", $days2);
		$this->tpl->assign("days3", $days3);
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
        $this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("total", $list["total"]);
        $this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("list",$list["list"]);
		$this -> tpl -> display("factory/goodsreport.tpl");
	}
	/*********
		工厂销售数据统计
	********/
	/*********************
	工厂商品销售报表
	定时跑数据，显示时间段内的销量
	************************/

	
	//定制衣服销量
	function factoryproduct()
	{
		$t1 = microtime(true);
		$this->db->query("TRUNCATE TABLE `db_factory_salereport` ");
		$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE 1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and  custom= 2 GROUP by goods_sku'.$limit;
		$query = $this->db->query($sql);
        $dates = $query->result_array();
		
		$post="";
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getcustom(2,$rsdb['goods_sku']);
				$num1 = $customNum[0]['orderNums']?$customNum[0]['orderNums']:0;
				$num2 = $customNum[1]['orderNums']?$customNum[1]['orderNums']:0;
				$num3 = $customNum[2]['orderNums']?$customNum[2]['orderNums']:0;
				if($num1==0 && $num2 ==0 && $num3==0)
				{
					continue;
				}
				$post[$keyst]['goos_sku'] = $rsdb['goods_sku'];
				$post[$keyst]['goods_name'] = $rsdb['skuName'];
				$post[$keyst]['goods_id'] = $rsdb['skuID'];
				$post[$keyst]['custom'] =2;
				$post[$keyst]['model'] =0;
				$post[$keyst]['size'] =0;
				$post[$keyst]['gender'] =0;
				$post[$keyst]['num1'] =$num1;
				$post[$keyst]['num2'] =$num2;
				$post[$keyst]['num3'] =$num3;
				$post[$keyst]['uptimes'] =time();
		}
		$this->db->insert_batch('db_factory_salereport', $post);
		$t2 = microtime(true);
		$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
		echo $vals;
		echo "OK";
	}
	

	//欧美男性
	function Europeorder()
	{
		//均码并且是欧美
		//custom 1均码 2定制
		//model  1欧美 2亚洲
		//gender 1男 2女 3通用
		$t1 = microtime(true);
		$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE  1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and  custom= 1  AND model=1 GROUP by goods_sku'.$limit;
		$query = $this->db->query($sql);
        $dates = $query->result_array();
		
		$post="";
		$newpost=array();
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getEuropeorder(1,$rsdb['goods_sku']);
				$post="";
				$i = 0;
				foreach($customNum as $key=>$rs)
				{
					
					if($rs['orderNums1']==0 && $rs['orderNums2'] ==0 && $rs['orderNums3'] ==0)
					{
						continue;
					}else{
						$post[$i]['goos_sku'] = $rsdb['goods_sku'];
						$post[$i]['goods_name'] = $rsdb['skuName'];
						$post[$i]['goods_id'] = $rsdb['skuID'];
						$post[$i]['custom'] =1;
						$post[$i]['model'] =1;
						$post[$i]['size'] =$rs['size'];
						$post[$i]['gender'] =1;
						$post[$i]['num1'] =$rs['orderNums1'];
						$post[$i]['num2'] =$rs['orderNums2'];
						$post[$i]['num3'] =$rs['orderNums3'];
						$post[$i]['uptimes'] =time();
					}
					$i=$i+1;
				}
				if(count($post)==1)
				{
					$newpost[] =$post[0];
				}else{
					foreach($post as $rst)
					{
						if($rst){
							$newpost[] = $rst;
						}
					}
				}
				$newpost = array_filter($newpost);
		}
		$res = $this->db->insert_batch('db_factory_salereport', $newpost);
		
		$t2 = microtime(true);
		$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
		echo $vals;
		echo "OK";

		die;
	}

	//欧美女性
	function Europenvorder()
	{
		$t1 = microtime(true);
		$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE  1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and custom= 1  AND model=1 GROUP by goods_sku'.$limit;
		$query = $this->db->query($sql);
        $dates = $query->result_array();
		
		$post="";
		$newpost=array();
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getEuropeorder(2,$rsdb['goods_sku']);
				$post="";
				$i = 0;
				foreach($customNum as $key=>$rs)
				{
					
					if($rs['orderNums1']==0 && $rs['orderNums2'] ==0 && $rs['orderNums3'] ==0)
					{
						continue;
					}else{
						$post[$i]['goos_sku'] = $rsdb['goods_sku'];
						$post[$i]['goods_name'] = $rsdb['skuName'];
						$post[$i]['goods_id'] = $rsdb['skuID'];
						$post[$i]['custom'] =1;
						$post[$i]['model'] =1;
						$post[$i]['size'] =$rs['size'];
						$post[$i]['gender'] =2;
						$post[$i]['num1'] =$rs['orderNums1'];
						$post[$i]['num2'] =$rs['orderNums2'];
						$post[$i]['num3'] =$rs['orderNums3'];
						$post[$i]['uptimes'] =time();
					}
					$i=$i+1;
				}
				if(count($post)==1)
				{
					$newpost[] =$post[0];
				}else{
					foreach($post as $rst)
					{
						if($rst){
							$newpost[] = $rst;
						}
					}
				}
				$newpost = array_filter($newpost);
		}
		$res = $this->db->insert_batch('db_factory_salereport', $newpost);
		
		
		$t2 = microtime(true);
		$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
		echo $vals;
		echo "OK";

	}
		//欧美通用
		function Europetongyongorder()
		{
			//均码并且是欧美
			
			$t1 = microtime(true);
			$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE 1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and  custom= 1  AND model=1 GROUP by goods_sku'.$limit;
			$query = $this->db->query($sql);
			$dates = $query->result_array();
		
			$post="";
		$newpost=array();
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getEuropeorder(3,$rsdb['goods_sku']);
				$post="";
				$i = 0;
				foreach($customNum as $key=>$rs)
				{
					
					if($rs['orderNums1']==0 && $rs['orderNums2'] ==0 && $rs['orderNums3'] ==0)
					{
						continue;
					}else{
						$post[$i]['goos_sku'] = $rsdb['goods_sku'];
						$post[$i]['goods_name'] = $rsdb['skuName'];
						$post[$i]['goods_id'] = $rsdb['skuID'];
						$post[$i]['custom'] =1;
						$post[$i]['model'] =1;
						$post[$i]['size'] =$rs['size'];
						$post[$i]['gender'] =3;
						$post[$i]['num1'] =$rs['orderNums1'];
						$post[$i]['num2'] =$rs['orderNums2'];
						$post[$i]['num3'] =$rs['orderNums3'];
						$post[$i]['uptimes'] =time();
					}
					$i=$i+1;
				}
				if(count($post)==1)
				{
					$newpost[] =$post[0];
				}else{
					foreach($post as $rst)
					{
						if($rst){
							$newpost[] = $rst;
						}
					}
				}
				$newpost = array_filter($newpost);
		}
		$res = $this->db->insert_batch('db_factory_salereport', $newpost);
		
		$t2 = microtime(true);
		$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
		echo $vals;
		echo "OK";
		
		}


		//亚洲男性
		function YzEuropeorder()
		{
			$t1 = microtime(true);
			$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE 1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and  custom= 1  AND model=2 GROUP by goods_sku'.$limit;
			$query = $this->db->query($sql);
			$dates = $query->result_array();
		
			$post="";
		$newpost=array();
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getEuropeorder(1,$rsdb['goods_sku']);
				$post="";
				$i = 0;
				foreach($customNum as $key=>$rs)
				{
					
					if($rs['orderNums1']==0 && $rs['orderNums2'] ==0 && $rs['orderNums3'] ==0)
					{
						continue;
					}else{
						$post[$i]['goos_sku'] = $rsdb['goods_sku'];
						$post[$i]['goods_name'] = $rsdb['skuName'];
						$post[$i]['goods_id'] = $rsdb['skuID'];
						$post[$i]['custom'] =1;
						$post[$i]['model'] =2;
						$post[$i]['size'] =$rs['size'];
						$post[$i]['gender'] =1;
						$post[$i]['num1'] =$rs['orderNums1'];
						$post[$i]['num2'] =$rs['orderNums2'];
						$post[$i]['num3'] =$rs['orderNums3'];
						$post[$i]['uptimes'] =time();
					}
					$i=$i+1;
				}
				if(count($post)==1)
				{
					$newpost[] =$post[0];
				}else{
					foreach($post as $rst)
					{
						if($rst){
							$newpost[] = $rst;
						}
					}
				}
				$newpost = array_filter($newpost);
		}
		$res = $this->db->insert_batch('db_factory_salereport', $newpost);
			$t2 = microtime(true);
			$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
			echo $vals;
			echo "OK";
		}

		//欧美女性
		function YzEuropenvorder()
		{
			
			$t1 = microtime(true);
			$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE 1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and  custom= 1  AND model=2 GROUP by goods_sku'.$limit;
			$query = $this->db->query($sql);
			$dates = $query->result_array();
		
			$post="";
		$newpost=array();
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getEuropeorder(2,$rsdb['goods_sku']);
				$post="";
				$i = 0;
				foreach($customNum as $key=>$rs)
				{
					
					if($rs['orderNums1']==0 && $rs['orderNums2'] ==0 && $rs['orderNums3'] ==0)
					{
						continue;
					}else{
						$post[$i]['goos_sku'] = $rsdb['goods_sku'];
						$post[$i]['goods_name'] = $rsdb['skuName'];
						$post[$i]['goods_id'] = $rsdb['skuID'];
						$post[$i]['custom'] =1;
						$post[$i]['model'] =2;
						$post[$i]['size'] =$rs['size'];
						$post[$i]['gender'] =2;
						$post[$i]['num1'] =$rs['orderNums1'];
						$post[$i]['num2'] =$rs['orderNums2'];
						$post[$i]['num3'] =$rs['orderNums3'];
						$post[$i]['uptimes'] =time();
					}
					$i=$i+1;
				}
				if(count($post)==1)
				{
					$newpost[] =$post[0];
				}else{
					foreach($post as $rst)
					{
						if($rst){
							$newpost[] = $rst;
						}
					}
				}
				$newpost = array_filter($newpost);
		}
		$res = $this->db->insert_batch('db_factory_salereport', $newpost);
			$t2 = microtime(true);
			$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
			echo $vals;
			echo "OK";
			
		}
		//欧美通用
		function YzEuropetongyongorder()
		{
			//均码并且是欧美
		$t1 = microtime(true);
			$sql= 'SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE 1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) and  custom= 1  AND model=2 GROUP by goods_sku'.$limit;
			$query = $this->db->query($sql);
			$dates = $query->result_array();
		
			$post="";
		$newpost=array();
		foreach($dates as $keyst=>$rsdb)
		{
				$customNum = $this->m->getEuropeorder(3,$rsdb['goods_sku']);
				$post="";
				$i = 0;
				foreach($customNum as $key=>$rs)
				{
					
					if($rs['orderNums1']==0 && $rs['orderNums2'] ==0 && $rs['orderNums3'] ==0)
					{
						continue;
					}else{
						$post[$i]['goos_sku'] = $rsdb['goods_sku'];
						$post[$i]['goods_name'] = $rsdb['skuName'];
						$post[$i]['goods_id'] = $rsdb['skuID'];
						$post[$i]['custom'] =1;
						$post[$i]['model'] =2;
						$post[$i]['size'] =$rs['size'];
						$post[$i]['gender'] =3;
						$post[$i]['num1'] =$rs['orderNums1'];
						$post[$i]['num2'] =$rs['orderNums2'];
						$post[$i]['num3'] =$rs['orderNums3'];
						$post[$i]['uptimes'] =time();
					}
					$i=$i+1;
				}
				if(count($post)==1)
				{
					$newpost[] =$post[0];
				}else{
					foreach($post as $rst)
					{
						if($rst){
							$newpost[] = $rst;
						}
					}
				}
				$newpost = array_filter($newpost);
		}
			$res = $this->db->insert_batch('db_factory_salereport', $newpost);
			$t2 = microtime(true);
			$vals='执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
			echo $vals;
			echo "OK";
		}

	public function ajaxbutton(){
		$op = $this->input->get_post('op');
	}


}
